package com.nbsaas.boot.pay.utils;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

/**
 * Created by ada on 2017/7/29.
 */
public class MapUtils {

  /**
   * 获取一个按照key排序的map
   *
   */
  public static Map<String, String> getSortMap() {
      // 升序排序
      // 升序排序
      return new TreeMap<>(
              String::compareTo);
  }

  /**
   * 拼接url参数
   *
   */
  public static String params(Map<String, String> map) {
    StringBuilder buffer = new StringBuilder();
    Set<String> keySet = map.keySet();
    Iterator<String> iter = keySet.iterator();
    if (iter.hasNext()) {
      String key = iter.next();
      buffer.append(key);
      buffer.append("=");
      buffer.append(map.get(key));
    }
    while (iter.hasNext()) {
      String key = iter.next();
      buffer.append("&");
      buffer.append(key);
      buffer.append("=");
      buffer.append(map.get(key));
    }
    return buffer.toString();
  }
}
